<template>

	<view>
		<!-- 拖动菜单 -->
		<view id="_drag_button" class="drag" :style="'left: ' + left + 'px; top:' + top + 'px;'"
			@touchstart="touchstart" @touchmove.stop.prevent="touchmove" @touchend.stop="touchend"
			@click.stop.prevent="click" :class="{ transition: isDock && !isMove }">
			<text>{{ text }}</text>
		</view>
		<u-action-sheet class="movable-sheet" round="20" @select="onSelect" @close="onClose" :closeOnClickOverlay="true"
			safeAreaInsetBottom="true" :actions="sheetList" :title="' '" :show="sheetShow"></u-action-sheet>
	</view>
</template>

<script>
	import {
		eventTypeList
	} from '@/utils/common.js'
	export default {
		name: 'drag-button',
		props: {
			isDock: {
				type: Boolean,
				default: false
			},
			existTabBar: {
				type: Boolean,
				default: false
			},
			evtType: {
				default: '',
				type: String
			},
			y: {
				default: 160,
				type: Number
			},
			ids: {
				default: "",
				type: String
			},
			actionData: {
				type: Array,
				default: []
			}
		},
		data() {
			return {
				sheetShow: false,
				movable: {
					x: 400,
					y: 160
				},
				sheetList: [],
				top: 0,
				left: 0,
				width: 0,
				height: 0,
				offsetWidth: 0,
				offsetHeight: 0,
				windowWidth: 0,
				windowHeight: 0,
				isMove: true,
				edge: 10,
				text: '菜单' //此处自定义按钮文字(1-3个汉字)
			};
		},
		mounted() {
			const sys = uni.getSystemInfoSync();

			this.windowWidth = sys.windowWidth;
			this.windowHeight = sys.windowHeight;

			// #ifdef APP-PLUS
			this.existTabBar && (this.windowHeight -= 50);
			// #endif
			if (sys.windowTop) {
				this.windowHeight += sys.windowTop;
			}
			// console.log(sys);
			const query = uni.createSelectorQuery().in(this);
			query
				.select('#_drag_button')
				.boundingClientRect(data => {
					this.width = data.width;
					this.height = data.height;
					this.offsetWidth = data.width / 2;
					this.offsetHeight = data.height / 2;
					this.left = this.windowWidth - this.width - this.edge;
					this.top = ((this.windowHeight - this.height - this.edge) * 3) / 4;
				})
				.exec();
		},
		methods: {
			// 加载上拉菜单
			loadSheetList() {
				// 默认
				switch (this.evtType) {
					case eventTypeList.OVERTIME:
					case eventTypeList.LEAVE:
						// 加班 请假
						this.sheetList = [{
								name: '历史记录',
							},
							{
								name: '公司制度',
							},
							{
								name: '相关法律'
							}
						]
						break;
					case eventTypeList.OTHER:
						// 其它申请
					case eventTypeList.BORROW:
					case eventTypeList.ON_BUSINESS:
					case eventTypeList.REIMBURSE:
					case eventTypeList.CLOCK:
						// 补卡,出差,报销,借款
						this.sheetList = [{
								name: '历史记录',
							},
							{
								name: '公司制度',
							},
						]
						break;
					case eventTypeList.PLAN:
						this.sheetList = [{
								name: '修改计划',
							},
							{
								name: '终止计划',
							},
						]
						break;
					default:
						// 补卡

						break;
				}
			},
			onSheetShow() {
				this.sheetShow = true
				this.$emit('evtShow', this.sheetShow)
			},
			onClose() {
				this.sheetShow = false
				this.$emit('evtShow', this.sheetShow)
			},
			onSelect(e) {
				if (this.actionData.length > 0) {
					this.$emit('select', e)
					return
				}
				switch (e.name) {
					case '历史记录':
						// if (this.evtType == eventTypeList.CLOCK) {
						// 	this.$Jump.Open('/packageA/approval/applyHisTwo?evtType=' + this.evtType)
						// } else {
						this.$Jump.Open('/packageA/approval/applyHis?evtType=' + this.evtType)
						// }
						break;
					case '公司制度':
						this.$Jump.Open('/packageA/approval/companySystem?evtType=' + this.evtType)
						break;
					case '相关法律':
						this.$Jump.Open('/packageA/approval/companySystem?evtType=' + this.evtType + "&type=law")
						break;
					case '终止计划':
						this.$Jump.Open('/packageA/workPlan/terminationPlan?evtType=' + this.evtType + '&id=' + this.ids)
						break;
					case '修改计划':
						this.$emit('select', e)
						break;	
						
					default:
						break;
				}
				console.log(e, this.evtType)
			},
			click() {
				if (this.actionData.length == 0) {
					this.loadSheetList()
				} else {
					this.sheetList = this.actionData
				}
				
				this.onSheetShow()
				console.log(1);
				this.$emit('btnClick');
			},
			touchstart(e) {
				this.$emit('btnTouchstart');
			},
			touchmove(e) {
				// 单指触摸
				if (e.touches.length !== 1) {
					return false;
				}

				this.isMove = true;

				this.left = e.touches[0].clientX - this.offsetWidth;

				let clientY = e.touches[0].clientY - this.offsetHeight;
				// #ifdef H5
				clientY += this.height;
				// #endif
				let edgeBottom = this.windowHeight - this.height - this.edge;

				// 上下触及边界
				if (clientY < this.edge) {
					this.top = this.edge;
				} else if (clientY > edgeBottom) {
					this.top = edgeBottom;
				} else {
					this.top = clientY;
				}
			},
			touchend(e) {
				if (this.isDock) {
					let edgeRigth = this.windowWidth - this.width - this.edge;

					if (this.left < this.windowWidth / 2 - this.offsetWidth) {
						this.left = this.edge;
					} else {
						this.left = edgeRigth;
					}
				}

				this.isMove = false;

				this.$emit('btnTouchend');
			}
		}
	};
</script>

<style lang="scss">
	.drag {
		display: flex;
		justify-content: center;
		align-items: center;
		background-color: rgba(0, 0, 0, 0.5);
		box-shadow: 0 0 6upx rgba(0, 0, 0, 0.4);
		color: $uni-text-color-inverse;
		width: 100upx;
		height: 100upx;
		border-radius: 50%;
		font-size: $uni-font-size-sm;
		position: fixed;
		z-index: 99;

		&.transition {
			transition: left 0.3s ease, top 0.3s ease;
		}
	}
</style>
